﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Data;

using EditorialAdministration;
using AutomationAdmin.ViewModels;

namespace AutomationAdmin.Views
{
    public partial class AutomationAppListView : UserControl
    {
        public AutomationAppListView()
        {
            
            InitializeComponent();
            this.DataContext = App.CurrentAutomationAppList;
        }

        private void CollapseRowGroup(DataGrid dataGrid)
        {
            if (dataGrid == null)
                return;

            AutomationAppList appList = this.DataContext as AutomationAppList;
            if (appList == null)
                return;

            if (appList.AutomationAppsView.Groups != null && appList.AutomationAppsView.Groups.Count > 0)
            {
                foreach (CollectionViewGroup group in appList.AutomationAppsView.Groups)
                {
                    dataGrid.ScrollIntoView(group, null);
                    dataGrid.CollapseRowGroup(group, true);
                }
            }
        }

        private bool _isJustGrouped = false;

        private void CheckBox_Checked(object sender, RoutedEventArgs e)
        {
            _isJustGrouped = true;
        }

        private void _listAutomationApps_LoadingRowGroup(object sender, DataGridRowGroupHeaderEventArgs e)
        {
            //_isJustGrouped = true;
        }

        private void _listAutomationApps_LayoutUpdated(object sender, EventArgs e)
        {
            AutomationAppList appList = this.DataContext as AutomationAppList;
            if (appList == null)
                return;

            if (_isJustGrouped)
            {
                CollapseRowGroup(_listAutomationApps);
                _isJustGrouped = false;
            }
        }

        private void _listAutomationApps_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            FillDataGridColumns(sender as DataGrid);
        }

        private static void FillDataGridColumns(DataGrid dataGrid)
        {
            if (dataGrid == null)
                return;

            // Do not change column size if Visibility State Changed
            if (dataGrid.RenderSize.Width != 0)
            {
                double all_columns_sizes = 0.0;
                foreach (DataGridColumn dg_c in dataGrid.Columns)
                {
                    all_columns_sizes += dg_c.ActualWidth;
                }
                // Space available to fill ( -18 Standard vScrollbar)
                double space_available = (dataGrid.RenderSize.Width - 18) - all_columns_sizes;
                foreach (DataGridColumn dg_c in dataGrid.Columns)
                {
                    dg_c.Width = new DataGridLength(dg_c.ActualWidth + (space_available / dataGrid.Columns.Count));
                }
            }
        }

        private void AddUsers_Click(object sender, RoutedEventArgs e)
        {
            AutomationApp cur = App.CurrentAutomationAppList.AutomationAppsView.CurrentItem as AutomationApp;
            if(cur != null)
                App.CurrentAppState.NavPath = "/Views/UserPermissionPage.xaml?AutomationAppId=" + cur.Id.ToString();
        }

        private void RemoveUsers_Click(object sender, RoutedEventArgs e)
        {
            AutomationApp cur = App.CurrentAutomationAppList.AutomationAppsView.CurrentItem as AutomationApp;
            if (cur != null)
                App.CurrentAppState.NavPath = "/Views/UserListPage.xaml?AutomationAppId=" + cur.Id.ToString();
        }

        private void RequestAccess_Click(object sender, RoutedEventArgs e)
        {
            AutomationApp cur = App.CurrentAutomationAppList.AutomationAppsView.CurrentItem as AutomationApp;
            if (cur != null)
                App.CurrentAppState.NavPath = "/Views/AutomationAppAccessReqPage.xaml?AutomationAppId=" + cur.Id.ToString();
        }

        private void Configuration_Click(object sender, RoutedEventArgs e)
        {
            AutomationApp cur = App.CurrentAutomationAppList.AutomationAppsView.CurrentItem as AutomationApp;
            if (cur != null)
                App.CurrentAppState.NavPath = "/Views/AutomationAppCfgPage.xaml?AutomationAppId=" + cur.Id.ToString();
        }

        private void Remove_Click(object sender, RoutedEventArgs e)
        {
            
        }
    }
}
